package cn.zy.gvs.v1.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zy.gvs.v1.mapper.GoViewProjectMapper;
import cn.zy.gvs.v1.model.GoViewProject;
import cn.zy.gvs.v1.model.GoViewProjectData;
import cn.zy.gvs.v1.model.vo.GoViewProjectVo;
import cn.zy.gvs.v1.service.IGoViewProjectDataService;
import cn.zy.gvs.v1.service.IGoViewProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yann
 */
@Service
@RequiredArgsConstructor
public class GoViewProjectServiceImpl extends ServiceImpl<GoViewProjectMapper, GoViewProject>
        implements IGoViewProjectService {

    private final IGoViewProjectDataService projectDataService;

    @Override
    public GoViewProject saveProject(GoViewProject goViewProject) {
        // 当前时间
        goViewProject.setCreateTime(DateUtil.now());
        // 默认: 未发布
        goViewProject.setState(-1);
        baseMapper.insert(goViewProject);
        return goViewProject;
    }

    @Override
    public GoViewProjectVo getProjectById(String projectId) {

        GoViewProjectData projectData = projectDataService.lambdaQuery().eq(GoViewProjectData::getProjectId, projectId)
                .one();
        if (ObjectUtil.isNull(projectData)) {
            return null;
        }
        GoViewProject goViewProject = baseMapper.selectById(projectId);

        GoViewProjectVo vo = new GoViewProjectVo();
        BeanUtil.copyProperties(goViewProject, vo);
        vo.setContent(projectData.getContent());
        return vo;
    }
}
